import vuex from 'vuex'
import Vue from 'vue';

Vue.use(vuex);
 
const actions = {
    jia(context,value) {
        console.log('action中的参数',context,value);
        context.commit('JIA',value);
    },
    jian(context,value) {
        console.log('action中的参数',context,value);
        context.commit('JIAN',value);
    }
};

const mutations = {
    JIA(state,value) {
        console.log('mutations中的参数',state,value);
        state.sum += value;
    },
    JIAN(state,value) {
        console.log('mutations中的参数',state,value);
        state.sum -= value;
    },
    ADD_PERSON(state,value) {
        state.personList.push(value);
    }
};

//getters 用于加工state的数据，复杂逻辑
const getters = {
    complicatedLogic(state) {
        return state.sum*10;
    }
}

const state = {
    sum:0,
    school:'SFU',
    subject:'Front-end',
    personList:[
        {id:'001',name:'Steven'},
        {id:'002',name:'Tim'},
        {id:'003',name:'Harry'}
    ]
};

const store = new vuex.Store({
    actions,
    mutations,
    state,
    getters
})

export default store;